import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# sns.set(style="white")
# plt.rcParams['font.sans-serif'] = "Simsun"  # 支持中文字体

# 定义文件路径
file_path = "D:\文档\DLMU\HLJU实验结果1.xlsx"

# 读取数据
try:
    data = pd.read_excel(file_path, sheet_name='Sheet2')
except Exception as e:
    print("读取 Excel 文件时出错:", e)
    exit()

# 检查必要的列是否存在
required_columns = ["Num", "T1", "T2", "T4"]
if not all(col in data.columns for col in required_columns):
    print("数据中缺少必要的列：", required_columns)
    exit()

# 去除完全空的行
data = data.dropna(how="all")

# 初始化绘图
plt.figure(figsize=(12, 8))

# 绘制 T4 的折线图
sns.lineplot(
    data=data,
    x="Num",
    y="T4",
    label="MOGRO",
    ci=None,
    linewidth=3,
    marker="o",
    markersize=8,
    color="#E60000",
    # alpha=0.8,
)
# 绘制 T2 的折线图
sns.lineplot(
    data=data,
    x="Num",
    y="T2",
    label="NSGA-II",
    ci=None,
    linewidth=3,
    marker="s",
    markersize=8,
    color="#007FFF",
    # alpha=0.8,
)
# 绘制 T1 的折线图
sns.lineplot(
    data=data,
    x="Num",
    y="T1",
    label="MOPSO",
    ci=None,
    linewidth=3,
    marker="^",
    markersize=9,
    color="#FFD700",
    # alpha=0.8,
)

# 设置图表标题和坐标轴标签
# plt.title("MOLSO 和 MOGRO 时间的对比", fontsize=16)
plt.xlabel("Number of teaching assignments", fontsize=16, fontname="SimHei", labelpad=10)
plt.ylabel("times(h)", fontsize=16, fontname="SimHei", labelpad=15)
# plt.xticks(data["Num"], fontsize=12)  # X轴刻度调整
xticks = np.linspace(30, 390, num=7)  # 6 个均匀间隔的刻度
plt.xticks(xticks.astype(int), fontsize=14, fontname="Times New Roman")  # 确保刻度为整数
plt.yticks(fontsize=14, fontname="Times New Roman")  # X轴刻度调整
# plt.xlim(data["Num"].min() - 5, data["Num"].max() + 5)  # 设置 x 轴范围
plt.ylim(data[["T1", "T2"]].min().min() - 0.5, data[["T1", "T2"]].max().max() + 0.5)  # 设置 y 轴从 0 开始并留一定余量

# 显示图例
plt.legend(title_fontsize=14, fontsize=10, loc='lower right', prop={'size': 14, "family": "Times New Roman"})

# 设置刻度线样式
plt.tick_params(axis='both', which='both', length=6, width=1, colors='black')  # 设置刻度线的样式

plt.grid(False)  # 关闭网格线
plt.tight_layout()

# 显示图形
plt.show()
